package com.example.demo3.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo3.entity.User;
import com.example.demo3.service.UserService;
import com.example.demo3.mapper.UserMapper;
import com.example.demo3.util.RedisUtil;
import com.example.demo3.util.Result;
import com.example.demo3.util.TokenUtil;


import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @author JYY
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-09-18 10:15:34
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<User> select() {
        return userMapper.selectList(null);
    }

    @Override
    public Result login(User user) {
        //数据库查询
        //先对密码进行加密,然后再做查询
        user.setUserPassword(SecureUtil.md5(user.getUserPassword()));
        //1,查询器
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account", user.getUserAccount())
                .eq("user_password", user.getUserPassword());

        //2,查询
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null) {
            //数据库验证成功,创建token
            TokenUtil tokenUtil = new TokenUtil();
            String token = tokenUtil.createToken(user1);
            //保存到Redis
            //将对象转换成json字符串
            Gson gson = new Gson();
            String json = gson.toJson(user1);
//            redisUtil.set(token,json);
            redisUtil.setWithExpire(token, json, 10000);
            return new Result(200, "登录成功,返回token字符串", token);
        }
        return new Result(200, "登陆失败,返回空值", null);
    }

    @Override
    public Result register(User user) {
        //加密
        user.setUserPassword(SecureUtil.md5(user.getUserPassword()));
        int num = userMapper.insert(user);
        return new Result(200, "注册成功", num);
    }

    @Override
    public Result exit(String token) {
        redisUtil.delete(token);
        return new Result(200 , "退出成功", null);
    }

    @Override
    public Result selectList(User user) {
        System.out.println("---------------"+user);
//创建分页对象
        Page page =new Page(user.getPage(),user.getSize());
//创建对应的查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(user.getUserName()!=null){
            queryWrapper.like("user_name",user.getUserName());
        }
        if(user.getUserAccount()!=null){
            queryWrapper.like("user_account",user.getUserAccount());
        }
        if(user.getUserPhone()!=null){
            queryWrapper.eq("user_phone",user.getUserPhone());
        }
//执行分页查询
        userMapper.selectPage(page,queryWrapper);
//获取分页查询结果
        List<User> list = page.getRecords();
//返回需要查询结果以及总行数，所以用map
        HashMap<String,Object> map = new HashMap<String,Object>();
//查询结果
        map.put("list",list);
//总行数
        map.put("total",page.getTotal());
        return new Result(200,"查询成功",map);
    }

    @Override
    public Result add(User user) {
        //用户是否存在？
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account",user.getUserAccount());
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1!=null){
            //已注册
            return new Result(503,"新增失败，用户已注册",0);
        }
        //加密
        user.setUserPassword(SecureUtil.md5(user.getUserPassword()));
        System.out.println("-----------"+user);
        int num = userMapper.insert(user);
        return new Result(200,"新增成功",1);
    }

    @Override
    public Result selectAccount(String userAccount) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account",userAccount);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return  new Result(500,"用户以存在",0);

        }
        return new Result(200,"用户不存在",1);
    }

    @Override
    public Result delect(Integer id) {
        //物理删除
        int num = userMapper.deleteById(id);

        //逻辑删除,更改用户状态,改成删除状态
        /*User user = new User();
        user.setUserStatus(0);
        user.setUserId((long) id);
        int num1 = userMapper.updateById(user);*/

        return new Result(200,"删除成功",num);
    }

    @Override
    public Result updateUser(User user) {
        int num = userMapper.updateById(user);
        return new Result(200,"修改成功",num);
    }
}




